﻿using System;
using System.Threading;
using System.Windows.Forms;
using Steamworks;
using Terraria.Localization;

namespace Terraria.Social.Steam
{
	// Token: 0x02000132 RID: 306
	public class CoreSocialModule : ISocialModule
	{
		// Token: 0x1400002D RID: 45
		// (add) Token: 0x06000D32 RID: 3378 RVA: 0x003F735C File Offset: 0x003F555C
		// (remove) Token: 0x06000D33 RID: 3379 RVA: 0x003F7390 File Offset: 0x003F5590
		public static event Action OnTick;

		// Token: 0x06000D34 RID: 3380 RVA: 0x003F73C4 File Offset: 0x003F55C4
		public void Initialize()
		{
			CoreSocialModule._instance = this;
			if (SteamAPI.RestartAppIfNecessary(new AppId_t(105600u)))
			{
				Environment.Exit(1);
				return;
			}
			if (!SteamAPI.Init())
			{
				MessageBox.Show(Language.GetTextValue("Error.LaunchFromSteam"), Language.GetTextValue("Error.Error"));
				Environment.Exit(1);
			}
			this.IsSteamValid = true;
			ThreadPool.QueueUserWorkItem(new WaitCallback(this.SteamCallbackLoop), null);
			ThreadPool.QueueUserWorkItem(new WaitCallback(this.SteamTickLoop), null);
			Main.OnTick += this.PulseSteamTick;
			Main.OnTick += this.PulseSteamCallback;
		}

		// Token: 0x06000D35 RID: 3381 RVA: 0x0000D3A8 File Offset: 0x0000B5A8
		public void PulseSteamTick()
		{
			if (Monitor.TryEnter(this._steamTickLock))
			{
				Monitor.Pulse(this._steamTickLock);
				Monitor.Exit(this._steamTickLock);
			}
		}

		// Token: 0x06000D36 RID: 3382 RVA: 0x0000D3CD File Offset: 0x0000B5CD
		public void PulseSteamCallback()
		{
			if (Monitor.TryEnter(this._steamCallbackLock))
			{
				Monitor.Pulse(this._steamCallbackLock);
				Monitor.Exit(this._steamCallbackLock);
			}
		}

		// Token: 0x06000D37 RID: 3383 RVA: 0x0000D3F2 File Offset: 0x0000B5F2
		public static void Pulse()
		{
			CoreSocialModule._instance.PulseSteamTick();
			CoreSocialModule._instance.PulseSteamCallback();
		}

		// Token: 0x06000D38 RID: 3384 RVA: 0x0000D408 File Offset: 0x0000B608
		private void SteamTickLoop(object context)
		{
			Monitor.Enter(this._steamTickLock);
			while (this.IsSteamValid)
			{
				if (CoreSocialModule.OnTick != null)
				{
					CoreSocialModule.OnTick();
				}
				Monitor.Wait(this._steamTickLock);
			}
			Monitor.Exit(this._steamTickLock);
		}

		// Token: 0x06000D39 RID: 3385 RVA: 0x0000D447 File Offset: 0x0000B647
		private void SteamCallbackLoop(object context)
		{
			Monitor.Enter(this._steamCallbackLock);
			while (this.IsSteamValid)
			{
				SteamAPI.RunCallbacks();
				Monitor.Wait(this._steamCallbackLock);
			}
			Monitor.Exit(this._steamCallbackLock);
			SteamAPI.Shutdown();
		}

		// Token: 0x06000D3A RID: 3386 RVA: 0x0000D47F File Offset: 0x0000B67F
		public void Shutdown()
		{
			Application.ApplicationExit += delegate(object obj, EventArgs evt)
			{
				this.IsSteamValid = false;
			};
		}

		// Token: 0x06000D3B RID: 3387 RVA: 0x0000D492 File Offset: 0x0000B692
		public void OnOverlayActivated(GameOverlayActivated_t result)
		{
			Main.instance.IsMouseVisible = (result.m_bActive == 1);
		}

		// Token: 0x040011B3 RID: 4531
		public const int SteamAppId = 105600;

		// Token: 0x040011B4 RID: 4532
		private static CoreSocialModule _instance;

		// Token: 0x040011B5 RID: 4533
		private bool IsSteamValid;

		// Token: 0x040011B6 RID: 4534
		private object _steamTickLock = new object();

		// Token: 0x040011B7 RID: 4535
		private object _steamCallbackLock = new object();

		// Token: 0x040011B8 RID: 4536
		private Callback<GameOverlayActivated_t> _onOverlayActivated;
	}
}
